/*!
    \file    main.c
    \brief   main routine		comb2

    \version 2021-08, V0.1
*/

#include <stdio.h>
#include "main.h"
#include "U2_i2c.h"
#include "U2_E3.h"
#include "U2_E1.h"
#include "U2_S1.h"
//#include "U2_S5.h"

extern unsigned char TUBE_NUMS[11][2];
static unsigned char loc;

//const unsigned char correct_password[20] = "cjjbiegoujiao\r\n";

i2c_addr_def  Door_E3_addr;
i2c_addr_def  Door_Key_addr;
i2c_addr_def  Door_TUBE_addr;
i2c_addr_def  Door_LED_addr;
//i2c_addr_def  Door_NFC_addr;

//unsigned char has_correct_nfc;
//unsigned char ucArray_ID[6];
//unsigned char DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char password[PASSWORD_LEN] = {1, 2, 3, 4};
unsigned char input[PASSWORD_LEN];

int main(void)
{
    printf("enter main\n");
    char dev_name[16];
    int i2c_fd;

    Door_E3_addr.flag = 0;
    Door_Key_addr.flag = 0;
    Door_TUBE_addr.flag = 0;
    Door_LED_addr.flag = 0;

    for (int i = I2C_DEV_NO_START; i < I2C_DEV_NUM + I2C_DEV_NO_START; ++i)
    {
        sprintf(dev_name, "%s%d", I2C_DEV_NAME_COMMON, i);
        printf("dev name is %s\n", dev_name);
        i2c_fd = I2c_open(dev_name);
        if (i2c_fd > 0)
        {
            printf("%s found\n", dev_name);

            Door_E3_addr = u2_e3_init(i2c_fd);
            if (Door_E3_addr.flag == 0)
                printf("E3 not found\n");

            Door_Key_addr = u2_s1_init(i2c_fd);
            if (Door_Key_addr.flag == 0)
                printf("key not found\n");

            Door_LED_addr = u2_e1_led_init(i2c_fd);
            if (Door_LED_addr.flag == 0)
                printf("LED not found\n");

            Door_TUBE_addr = u2_e1_tube_init(i2c_fd);
            if (Door_TUBE_addr.flag == 0)
                printf("TUBE not found\n");
        }
        else
        {
            printf("%s not found\n", dev_name);
        }
    }

    printf("open successfully\n");
    while (1)
    {
        //delay_1ms(1000);
        unsigned char res, ret;
        ret = u2_key_trans(Door_Key_addr, &res);
        input_number(res);
    }

}

unsigned char check_password(unsigned char input_len)
{
    unsigned char i;

    if (input_len != PASSWORD_LEN)
        return 0;

    for (i = 0; i < PASSWORD_LEN; ++i)
    {
        if (input[i] != password[i])
            return 0;
    }

    return 1;
}

void input_number(unsigned char number)
{
    switch(number)
    {
        case INPUT_1 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 1;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[1][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[1][1]);
            break;
        case INPUT_2 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 2;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[2][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[2][1]);
            break;
        case INPUT_3 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 3;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[3][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[3][1]);
            break;
        case INPUT_4 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 4;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[4][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[4][1]);
            break;
        case INPUT_5 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 5;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[5][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[6][1]);
            break;
        case INPUT_6 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 6;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[6][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[6][1]);
            break;
            case INPUT_7 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 7;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[7][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[7][1]);
            break;
        case INPUT_8 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 8;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[8][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[8][1]);
            break;
        case INPUT_9 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 9;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[9][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[9][1]);
            break;
        case INPUT_0 :
            if (loc == PASSWORD_LEN)
            {
                loc = 0;
                u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            }
            input[loc] = 0;
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc), TUBE_NUMS[0][0]);
            I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc++) + 1, TUBE_NUMS[0][1]);
            break;
        case INPUT_DELETE :
            if (loc != 0)
            {
                I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(--loc), TUBE_NUMS[10][0]);
                I2c_write_reg(Door_TUBE_addr.fd, Door_TUBE_addr.addr, POS(loc) + 1, TUBE_NUMS[10][1]);
            }
            break;
        case INPUT_CHECK :
            u2_ht16k33_RAM_init(Door_TUBE_addr.fd, Door_TUBE_addr.addr);
            if (check_password(loc))
            {
                u2_e3_set_position(Door_E3_addr.fd, Door_E3_addr.addr, OPEN);
                // delay_1ms(7000);
                u2_e3_set_position(Door_E3_addr.fd, Door_E3_addr.addr, CLOSE);
            }
            loc = 0;
            break;
        default : ;
//            if (has_correct_nfc == 1)
//            {
//                has_correct_nfc = 0;
//                u2_e3_set_position(Door_E3_addr.fd, Door_E3_addr.addr, OPEN);
//                // delay_1ms(7000);
//                u2_e3_set_position(Door_E3_addr.fd, Door_E3_addr.addr, CLOSE);
//            }
    }
}
